﻿//  ========================================================================
//  EveHQ - An Eve-Online™ character assistance application
//  Copyright © 2005-2012  EveHQ Development Team
//  
//  This file (WebRequestProvider.cs), is part of EveHQ.
// 
//  EveHQ is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 2 of the License, or
//  (at your option) any later version.
// 
//  EveHQ is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
// 
//  You should have received a copy of the GNU General Public License
//  along with EveHQ.  If not, see <http://www.gnu.org/licenses/>.
// =========================================================================

namespace EveHQ.EveApi
{
    using System;
    using System.Collections.Generic;
    using System.Diagnostics.CodeAnalysis;
    using System.Net.Http;
    using System.Threading.Tasks;

    /// <summary>
    /// Used to wrap the web requests, so they can be more easily mocked for Unit Tests
    /// </summary>
    public sealed class WebRequestProvider : IDisposable,IWebRequestProvider
    {
        /// <summary>
        /// http client object to use for communication.
        /// </summary>
        private readonly HttpClient _client = new HttpClient();

        /// <summary>
        /// Disposes this object and cleans up resources
        /// </summary>
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        /// <summary>
        /// Finalizes the object.
        /// </summary>
        ~WebRequestProvider()
        {
            Dispose(false);
        }

        /// <summary>
        /// Makes a request against a remote web service, using the HTTP GET verb.
        /// </summary>
        /// <param name="target">Url to request</param>
        /// <returns>A reference to the task</returns>
        [SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "Disposing this object before the end of the method scope would prevent the Task from completing")]
        public Task<HttpResponseMessage> GetAsync(Uri target)
        {
            return _client.GetAsync(target);
        }

        /// <summary>
        /// Sends a request to a remote web location, using the HTTP POST verb.
        /// </summary>
        /// <param name="target">Url to send to</param>
        /// <param name="postData">Collection of data to send on the request</param>
        /// <returns>A reference to the task</returns>
        [SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope",
            Justification = "Asynchronous operation. The content object cannot be disposed here or the operation will fail. It gets cleaned up when the httpClient object is disposed.")]
        public Task<HttpResponseMessage> PostAsync(Uri target, IDictionary<string, string> postData)
        {
            var content = new FormUrlEncodedContent(postData);
            return _client.PostAsync(target, content);
        }

        /// <summary>
        /// Disposes the object
        /// </summary>
        /// <param name="disposing">value indicating whether this was called from the finalizer or not.</param>
        private void Dispose(bool disposing)
        {
            if (disposing)
            {
                _client.Dispose();
            }
        }
    }
}